﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Aliyun.Acs.Core;
using Aliyun.Acs.Core.Exceptions;
using Aliyun.Acs.Core.Profile;
using Aliyun.Acs.vod;
using Aliyun.Acs.vod.Model.V20170321;
using Donal.Xms.Common;
using static Aliyun.Acs.vod.Model.V20170321.GetVideoInfoResponse.GetVideoInfo_Video;

namespace Donal.Xms.API.Cloud
{
    public static class AliyunVideoHelper
    {
        /// <summary>
        /// 刷新视频上传凭证
        /// </summary>
        /// <param name="AccessKeyId"></param>
        /// <param name="AccessKeySecret"></param>
        /// <param name="VideoId"></param>
        /// <returns></returns>
        public static AlibabaCloud.SDK.Vod20170321.Models.RefreshUploadVideoResponse RefreshUploadVideo(string AccessKeyId, string AccessKeySecret, string VideoId)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = AccessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = AccessKeySecret,
            };
            // 访问的域名
            config.Endpoint = "vod.cn-shanghai.aliyuncs.com";

            AlibabaCloud.SDK.Vod20170321.Client client = new AlibabaCloud.SDK.Vod20170321.Client(config);
            AlibabaCloud.SDK.Vod20170321.Models.RefreshUploadVideoRequest refreshUploadVideoRequest = new AlibabaCloud.SDK.Vod20170321.Models.RefreshUploadVideoRequest()
            {
                VideoId = VideoId
            };

            AlibabaCloud.SDK.Vod20170321.Models.RefreshUploadVideoResponse resp = client.RefreshUploadVideo(refreshUploadVideoRequest);

            return resp;
        }
        /// <summary>
        /// 获取视频上传地址和凭证
        /// </summary>
        /// <param name="AccessKeyId"></param>
        /// <param name="AccessKeySecret"></param>
        /// <param name="FileName"></param>
        /// <param name="Title"></param>
        /// <returns></returns>
        public static AlibabaCloud.SDK.Vod20170321.Models.CreateUploadVideoResponse CreateUploadVideoResponse(string AccessKeyId, string AccessKeySecret, string FileName, string Title)
        {
            AlibabaCloud.OpenApiClient.Models.Config config = new AlibabaCloud.OpenApiClient.Models.Config
            {
                // 您的AccessKey ID
                AccessKeyId = AccessKeyId,
                // 您的AccessKey Secret
                AccessKeySecret = AccessKeySecret,
            };
            // 访问的域名
            config.Endpoint = "vod.cn-shanghai.aliyuncs.com";

            AlibabaCloud.SDK.Vod20170321.Client client = new AlibabaCloud.SDK.Vod20170321.Client(config);
            AlibabaCloud.SDK.Vod20170321.Models.CreateUploadVideoRequest createUploadVideoRequest = new AlibabaCloud.SDK.Vod20170321.Models.CreateUploadVideoRequest()
            {
                FileName = FileName,//"H:\\嘟.mp4",
                Title = Title//"嘟"
            };

            AlibabaCloud.SDK.Vod20170321.Models.CreateUploadVideoResponse resp = client.CreateUploadVideo(createUploadVideoRequest);

            return resp;
        }

        public static Model.aliyun_video GetVideoInfoRequest(string VideoId, string AccessKeyId, string AccessKeySecret)
        {
            try
            {
                Model.aliyun_video aliyun_Video = new Model.aliyun_video();
                // 构造请求
                GetVideoInfoRequest request = new GetVideoInfoRequest();
                request.VideoId = VideoId;
                // 初始化客户端
                DefaultAcsClient client = InitVodClient(AccessKeyId, AccessKeySecret);
                // 发起请求，并得到 response
                GetVideoInfoResponse response = client.GetAcsResponse(request);

                if (response != null)
                {
                    aliyun_Video.AppId = response.Video.AppId;
                    aliyun_Video.VideoId = response.Video.VideoId;
                    aliyun_Video.AuditStatus = response.Video.AuditStatus;
                    aliyun_Video.CateId = response.Video.CateId;
                    aliyun_Video.CateName = response.Video.CateName;
                    aliyun_Video.CoverURL = response.Video.CoverURL;
                    aliyun_Video.CreateTime = response.Video.CreateTime;
                    aliyun_Video.CreationTime = response.Video.CreationTime;
                    aliyun_Video.CustomMediaInfo = response.Video.CustomMediaInfo;
                    aliyun_Video.Description = response.Video.Description;
                    aliyun_Video.DownloadSwitch = response.Video.DownloadSwitch;
                    aliyun_Video.Duration = response.Video.Duration;
                    aliyun_Video.ModificationTime = response.Video.ModificationTime;
                    aliyun_Video.ModifyTime = response.Video.ModifyTime;
                    aliyun_Video.PreprocessStatus = response.Video.PreprocessStatus;
                    aliyun_Video.RegionId = response.Video.RegionId;
                    aliyun_Video.Size = response.Video.Size;
                    aliyun_Video.Snapshots = response.Video.Snapshots;
                    aliyun_Video.Status = response.Video.Status;
                    aliyun_Video.StorageLocation = response.Video.StorageLocation;
                    aliyun_Video.Tags = response.Video.Tags;
                    aliyun_Video.TemplateGroupId = response.Video.TemplateGroupId;
                    aliyun_Video.ThumbnailList = GetList(response.Video.ThumbnailList);
                    aliyun_Video.Title = response.Video.Title;

                    return aliyun_Video;
                }

                return null;
            }
            catch (ServerException ex)
            {
                Utils.WriteLogs("GetVideoInfoRequest", ex.ToString());
                return null;
            }

        }

        private static List<string> GetList(List<GetVideoInfo_Thumbnail> ThumbnailList)
        {
            List<string> _list = new List<string>();
            if (ThumbnailList != null && ThumbnailList.Count > 0)
            {
                foreach (GetVideoInfo_Thumbnail gvit in ThumbnailList)
                {
                    _list.Add(gvit.URL);
                }
            }

            return _list;
        }


        public static DefaultAcsClient InitVodClient(string accessKeyId, string accessKeySecret)
        {
            // 点播服务接入区域
            string regionId = "cn-shanghai";
            IClientProfile profile = DefaultProfile.GetProfile(regionId, accessKeyId, accessKeySecret);
            // DefaultProfile.AddEndpoint(regionId, regionId, "vod", "vod." + regionId + ".aliyuncs.com");
            return new DefaultAcsClient(profile);
        }


        public static string GetVideoPlayAuth(string VideoId, string AccessKeyId, string AccessKeySecret)
        {
            try
            {
                // 构造请求
                GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
                request.VideoId = VideoId;
                //request.AuthInfoTimeout = 3000;
                DefaultAcsClient client = InitVodClient(AccessKeyId, AccessKeySecret);
                // 发起请求，并得到 response
                GetVideoPlayAuthResponse response = client.GetAcsResponse(request);

                if (response != null)
                {
                    return response.PlayAuth;
                }

            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return "";
        }

        public static string GetVideoPlayAuth(DefaultAcsClient client, string VideoId)
        {
            try
            {
                // 构造请求
                GetVideoPlayAuthRequest request = new GetVideoPlayAuthRequest();
                request.VideoId = VideoId;
                //request.AuthInfoTimeout = 3000;

                // 发起请求，并得到 response
                GetVideoPlayAuthResponse response = client.GetAcsResponse(request);

                if (response != null)
                {
                    return response.PlayAuth;
                }

            }
            catch (ServerException ex)
            {
                Console.WriteLine(ex.ToString());
            }

            return "";
        }


    }
}
